package com.raysweather.android;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class ForecastXMLHandler extends DefaultHandler {

	Boolean currentElement = false;
	String currentValue = null;
	public static ForecastList forecastList = null;
	private int day = 0;

	public static ForecastList getSitesList() {
		return forecastList;
	}

	public static void setSitesList(ForecastList sitesList) {
		ForecastXMLHandler.forecastList = sitesList;
	}

	/** Called when tag starts ( ex:- <name>AndroidPeople</name>
	 * -- <name> )*/
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		currentElement = true;

		if (localName.equals("forecast"))
		{
			/** Start */
			forecastList = new ForecastList();
		} 
		else if(localName.equals("day1"))
			day = 1;
		else if(localName.equals("day2")){
			day = 2;
			Log.d("#DAY","day2");
		}
		else if(localName.equals("day3"))
			day = 3;

	}

	/** Called when tag closing ( ex:- <name>AndroidPeople</name>
	 * -- </name> )*/
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		currentElement = false;
		/** set value */
		if (localName.equalsIgnoreCase("station_id"))
			forecastList.setId(currentValue);
		else if (localName.equalsIgnoreCase("time"))
			forecastList.setTime(currentValue);
		else if (localName.equalsIgnoreCase("title"))
			forecastList.setTitle(currentValue);
		else if (localName.equalsIgnoreCase("introduction"))
			forecastList.setIntro(currentValue);
		else if (localName.equalsIgnoreCase("snowman"))
			forecastList.setSnowman(currentValue);
		else if (localName.equalsIgnoreCase("day_of_week")){
			if(day == 1){
				forecastList.setD1Day(currentValue);
				Log.d("DAY1",currentValue);
			}
			else if(day == 2){
				forecastList.setD2Day(currentValue);
				Log.d("DAY2",currentValue);
			}
			else
				forecastList.setD3Day(currentValue);
		}else if (localName.equalsIgnoreCase("sky_condition")){
			if(day == 1)
				forecastList.setD1Sky(currentValue);
			else if(day == 2)
				forecastList.setD2Sky(currentValue);
			else
				forecastList.setD3Sky(currentValue);
		}else if (localName.equalsIgnoreCase("high_temperature")){
			if(day == 1)
				forecastList.setD1Hi(currentValue);
			else if(day == 2)
				forecastList.setD2Hi(currentValue);
			else
				forecastList.setD3Hi(currentValue);
		}else if (localName.equalsIgnoreCase("low_temperature")){
			if(day == 1)
				forecastList.setD1Lo(currentValue);
			else if(day == 2)
				forecastList.setD2Lo(currentValue);
			else
				forecastList.setD3Lo(currentValue);
		}else if (localName.equalsIgnoreCase("golfballs")){
			if(day == 1)
				forecastList.setD1Balls(currentValue);
			else if(day == 2)
				forecastList.setD2Balls(currentValue);
			else
				forecastList.setD3Balls(currentValue);
		}else if (localName.equalsIgnoreCase("forecast_text")){
			if(day == 1)
				forecastList.setD1Text(currentValue);
			else if(day == 2)
				forecastList.setD2Text(currentValue);
			else
				forecastList.setD3Text(currentValue);
		}
	}

	/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
	 * -- to get AndroidPeople Character ) */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}

	}

}
